
#include "max7219.h"
#include "charactersfont.c"


#define noop 0x00
#define digit0 0x01
#define digit1 0x02
#define digit2 0x03
#define digit3 0x04
#define digit4 0x05
#define digit5 0x06
#define digit6 0x07
#define digit7 0x08
#define decodemode 0x09
#define intensity 0x0A
#define scanlimit 0x0B
#define shutdown 0x0C
#define disptest 0x0F

int DIN, LOAD, CLK;




void MAX7219::setup(int a, int b, int c){
DIN = a;
LOAD = b;
CLK = c;


  pinMode(DIN,1);
  pinMode(LOAD, 1);
  pinMode(CLK, 1);
  digitalWrite(CLK,0);
  digitalWrite(LOAD,1); 

write(disptest,0x00);
  write(decodemode,0x00);
  write(intensity, 0x0F);
  write(scanlimit, 0x07);
  write(shutdown, 0x01);
}


void MAX7219::write(uint8_t address,uint8_t data, int number){
unsigned long tempdata = 0;
tempdata = address << 8;
tempdata = tempdata + data;
digitalWrite(LOAD,0);
for(int i = 0; i <= 15; i++){
  digitalWrite(CLK,0);
  digitalWrite(DIN,bitRead(tempdata,15));
  tempdata = tempdata << 1;
  digitalWrite(CLK,1);
}
if(number > 0){
for(int i = 0; i < number; i++){
for(int i = 0; i <= 15; i++){
  digitalWrite(CLK,0);
  digitalWrite(DIN,bitRead(tempdata,15));
  tempdata = tempdata << 1;
  digitalWrite(CLK,1);
}
}
}

digitalWrite(LOAD,1);

}
void MAX7219::write(uint8_t address,uint8_t data){
unsigned long tempdata = 0;
tempdata = address << 8;
tempdata = tempdata + data;
digitalWrite(LOAD,0);
for(int i = 0; i <= 15; i++){
  digitalWrite(CLK,0);
  digitalWrite(DIN,bitRead(tempdata,15));
  tempdata = tempdata << 1;
  digitalWrite(CLK,1);
}


digitalWrite(LOAD,1);

}

void MAX7219::pic(uint8_t pic[8]){
  write(digit0, pic[0]);
  write(digit1, pic[1]);
  write(digit2, pic[2]);
  write(digit3, pic[3]);
  write(digit4, pic[4]);
  write(digit5, pic[5]);
  write(digit6, pic[6]);
  write(digit7, pic[7]);
  
}
void MAX7219::pic(uint8_t pic[8],int nop){
  write(digit0, pic[0],nop);
  write(digit1, pic[1],nop);
  write(digit2, pic[2],nop);
  write(digit3, pic[3],nop);
  write(digit4, pic[4],nop);
  write(digit5, pic[5],nop);
  write(digit6, pic[6],nop);
  write(digit7, pic[7],nop);
  
}

void MAX7219::shiftright(uint8_t* address){
  int endbit;
  for(int i = 0; i <8; i++){
    endbit = address[i] & 1;
    address[i] = address[i] >> 1;
    address[i] = address[i] | (endbit * 128);
  }
  
}

void MAX7219::shiftleft(uint8_t* address){
int endbit = 0;
for(int i = 0; i < 8; i++){
endbit = bitRead(address[i],7);
address[i] = address[i] << 1;
address[i] = address[i] + endbit;
} 
} 

void MAX7219::rotate(uint8_t* address){
uint8_t original[8];
 for(int i = 0; i < 8; i++)original[i] = address[i];

for(int y=0; y < 8; y++) for(int x=0; x <8; x++) bitWrite(address[y],x,bitRead(original[x],7-y));


  
  
}

void MAX7219::raise(uint8_t* address){
 uint8_t original[8];
 for(int i = 0; i < 8; i++)  original[i] = address[i];
 for(int i = 0; i < 7; i++)  address[i] = original[i+1];
address[7] = original[0];

}
void MAX7219::lower(uint8_t* address){
 uint8_t original[8];
 for(int i = 0; i < 8; i++)  original[i] = address[i];
 for(int i = 1; i < 8; i++)  address[i] = original[i-1];
address[0] = original[7];

}



void MAX7219::flip(uint8_t* address){
  uint8_t original[8];
 for(int i = 0; i < 8; i++)  original[i] = address[i];
 for(int i = 0; i < 8; i++)  address[7-i] = original[i];
}

void MAX7219::drawchar(int c){
uint8_t _buffer[8];
if(c < 0) c = 256 + c;
for(int i = 0; i < 8; i++) _buffer[i] = pgm_read_byte(font+(c*8)+i);
//rotate(_buffer); //may or may not need these correcting factors
//shiftright(_buffer);
//flip(_buffer);
pic(_buffer);

}


void MAX7219::drawchar(int c, int nop){
uint8_t _buffer[8];
for(int i = 0; i < 8; i++) _buffer[i] = pgm_read_byte(font+(c*8)+i);
//rotate(_buffer);
//shiftright(_buffer);
//flip(_buffer);
pic(_buffer, nop);
}





